﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace PacCraft
{
    class Sprite3D
    {
        public Vector3 position;
        public Vector3 scale;
        public float alpha = 1f;
        public float rotationY;
        protected Model model;
        private Random r;

        public Sprite3D(Model newModel)
        {
            model = newModel;
            r = new Random();
            position = new Vector3(0f, 0f, 0f);
            scale = new Vector3(0.039f, 0.039f, 0.039f);
        }

        public void Draw(BasicEffect basicEffect, bool randomColor = false)
        {
            Matrix[] transforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    if (randomColor)
                        effect.DiffuseColor = new Vector3((float)r.NextDouble(), (float)r.NextDouble(), (float)r.NextDouble());
                    else
                        effect.DiffuseColor = new Vector3(1, 1, 1);
                    // Cool epileptic colors
                    // effect.SpecularColor = new Vector3((float)r.NextDouble(), (float)r.NextDouble(), (float)r.NextDouble());
                    // effect.SpecularPower = alpha;
                    effect.Alpha = alpha;
                    effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationY(rotationY) *  Matrix.CreateScale(scale) * Matrix.CreateTranslation(position);
                    effect.View = basicEffect.View;
                    effect.Projection = basicEffect.Projection;
                }
                mesh.Draw();
            }
        }
    }
}
